package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrBillDReService;
import cn.fl.mcr.service.IMcrBillMReService;
import cn.fl.mcr.vo.McrBillDReVO;
import cn.fl.mcr.vo.McrBillMReVO;
import cn.fl.mcr.vo.McrBillMVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrBillDReQC;
import cn.lionbridgecapital.loan.model.condition.McrBillMReQC;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 回购账单表 控制层
 */
@Controller
@RequestMapping("/mcr/mcrBillMRe")
public class McrBillMReController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(McrBillMReController.class);

    /**
     * McrBillM 服务层BEAN
     */
    @Autowired
    private IMcrBillMReService mcrBillMReService;
    @Autowired
    private IMcrBillDReService mcrBillDReService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage() {
        ModelAndView mav = new ModelAndView("mcr/mcrBillMRe/mcrBillMList");
        BaseLogUtils.info(logger, "toListPage", "加载McrBillMRe列表页面.", CurrentThreadContext.getCurrentUserName());
        return mav;
    }

    /**
     * 初始化列表
     */
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initMcrBillMReList(@RequestParam("page") Integer currentPage
            , @RequestParam("pagesize") Integer pageSize
            , @RequestParam(value = "cstNm", required = false) String cstNm
            , @RequestParam(value = "crCntNo", required = false) String crCntNo
            , @RequestParam(value = "repoSubject", required = false) String repoSubject
            , @RequestParam(value = "settle", required = false) String settle) {

        Pager page = getPager(currentPage, pageSize, cstNm, crCntNo, repoSubject, settle);
        BaseLogUtils.info(logger, "initMcrBillMReList", "初始化McrBillM列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.mcrBillMReService.searchListPage(page);
        List<McrBillMReVO> list = (List<McrBillMReVO>) pager.getResultList();
        ListPageVO<McrBillMReVO> listPage = new ListPageVO<>(list, pager.getTotalCount());
        BaseLogUtils.info(logger, "initMcrBillMReList", "返回页面信息 :" + JSONObject.toJSONString(listPage));
        return listPage;
    }

    /**
     * 得到一条记录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toBillDtlPage.do", method = {RequestMethod.GET})
    public ModelAndView toBillDtlPage(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        BaseLogUtils.info(logger, "toListPage", "加载McrBillMRE详情页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("mcr/mcrBillMRe/mcrBillDtl");
        String id = request.getParameter("id");
        if (StringUtils.isNotEmpty(id)) {
            McrBillMReVO vo = this.mcrBillMReService.selectById(Long.valueOf(id));
            BaseLogUtils.info(logger, "账单主信息", "加载账单主信息." + JSON.toJSONString(vo), CurrentThreadContext.getCurrentUserName());

            if (null != vo) {
                Date crDt = vo.getCrDt();
                vo.setCrDtString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(crDt));
                mav.addObject("mcrBillMVO", vo);
            }
            mav.addObject("billId", id);
        }
        return mav;
    }


    /**
     * 回购还款计划列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/billDReInitList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initMcrBillDList(@RequestParam("billId") String billId) {
        McrBillDReQC qc = new McrBillDReQC();
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        if (null == billId || "".equals(billId)) {
            return null;
        }
        qc.setBillId(Long.valueOf(billId));
        BaseLogUtils.info(logger, "initMcrBillDList", "初始化McrBillDRE列表页面数据.DATA:" + qc.toString(), CurrentThreadContext.getCurrentUserName());
        List<McrBillDReVO> list = this.mcrBillDReService.searchList(qc);

        BaseLogUtils.info(logger, "initMcrBillDList", "初始化McrBillDRE列表页面数据.DATA:" + list.size());
        if (list == null) {
            list = new ArrayList<>();
        }
        ListPageVO<McrBillDReVO> listPage = new ListPageVO<>(list, list.size());
        return listPage;
    }

    /**
     * 获取分页查询实体类
     * @param currentPage
     * @param pageSize
     * @return
     */
    private Pager getPager(Integer currentPage, Integer pageSize, String cstNm, String crCntNo, String repoSubject, String settle) {
        BaseLogUtils.info(logger, "initMcrBillMReList", "初始化McrBillM列表页面数据.DATA: cstNm :" + cstNm + ", crCntNo: " + crCntNo + " , repoSubject: " + repoSubject + ", settle:" + repoSubject  , CurrentThreadContext.getCurrentUserName());
        Pager page = new Pager();
        page.setCurrentPage(currentPage);
        page.setEveryPage(pageSize);

        McrBillMReQC qc = getMcrBillMQC(cstNm, crCntNo
                , StringUtils.isBlank(repoSubject) ? null : Integer.valueOf(repoSubject)
                , StringUtils.isBlank(settle) ? null : Integer.valueOf(settle));
        page.setCondition(qc);
        return page;
    }

    /**
     * 封装查询参数
     * @param cstNm
     * @param crCntNo
     * @return
     */
    private McrBillMReQC getMcrBillMQC(String cstNm, String crCntNo, Integer repoSubject, Integer settle) {
        McrBillMReQC qc = new McrBillMReQC();
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        qc.setCstNm(cstNm);
        qc.setCrCntNo(crCntNo);
        qc.setIsSettle(settle);
        qc.setRepoSubject(repoSubject);
        return qc;
    }


}

